// 异常处理
// Created by FanGccU on 2025/6/15.
//
#include <iostream>
using namespace std;


double division(int num1, int num2) {
    if (num2 == 0) {
        // 抛出自定义异常
        throw "被除数不能为0";
    }
    return num1 / num2;
}

void errFunc() {
    int a = 10;
    int b = 0;

    try {
        double c = division(a, b);
        cout << "value of c is: " << c << endl;
    } catch (const char *msg) {
        cerr << msg << endl;
    }
}

/**
 * 自定义异常
 */
struct MyException : public exception {
    const char *what() const throw() {
        return "C++ Exception";
    }
};

/**
 * 自定义异常
 */
void customExceptionFunc() {
    try {
        throw MyException();
        // 为什么要使用 & ？
        // 1、避免调用 拷贝函数
        // 2、使用引用可以避免拷贝对象，提高性能
    } catch (const MyException &e) {
        cerr << "My Exception" << endl;
        cerr << e.what() << endl;
    }catch (const exception &e) {
        cerr << "Other Exception" << endl;
    }
}


int main(int argc, char *argv[]) {
    // errFunc();
    customExceptionFunc();
}
